package com.fusion.core.comm.filetransfers;

import com.fusion.core.file.filedatabase.FileDescriptor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @author SwethaShivakumar 
 * Project: fusion
 * Date: Mar 6, 2009
 * URL: http://fusionwiki.co.cc
 * Mail Me At: shiva@fusionproject.co.cc
 */
public class FileDescriptorProvider implements DataProvider {
    private byte[] fd;
    private int index = 0;
    private boolean firstFill = true;

    public FileDescriptorProvider(FileDescriptor fd) throws IOException {
        if(T.t)T.debug("Getting ready to send a FD: "+fd);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        fd.serializeTo(out, true);
        out.flush();
        this.fd = out.toByteArray();
    }

    public int fill(ByteBuffer buf) {
        if (firstFill) {
            buf.putInt(fd.length);
            firstFill = false;
        }
        int len = fd.length-index;
        if(T.t)T.trace("len: "+len);
        if (len <= 0) {
            if(T.t)T.trace("Done providing file descriptor");
            return -1;
        }
        if (buf.remaining() < len) len = buf.remaining();
        buf.put(fd, index, len);
        index += len;
        if(T.t)T.trace("Provided with "+len+" bytes of FD");
        return len;
    }
}
